<template>
  <div class="common-layout">
    <el-form class="header" :model="queryParam" ref="queryRef" :inline="true">

      <el-form-item label="组号" prop="group">
        <el-input clearable v-model="groupNum"
                  style="width: 200px;"
                  @blur="inputKeyUpEvent"
                  placeholder="请输入组号(1-89)"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="searchByUnit">查询</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="20">
      <el-col :sm="24" :lg="24" style="padding-left: 20px;padding-right: 20px">
        <el-table border :data="wordTestList">
          <el-table-column label="组号" align="center" prop="complete"/>
          <el-table-column label="学习个数" align="center" prop="correctNum"/>
          <el-table-column label="总数" align="center" prop="wordNum"/>
          <el-table-column label="完成率" align="center" prop="correctRate">
            <template #default="scope">
              <span>{{ parseInt(scope.row.correctRate * 100).toFixed(0)}}%</span>
            </template>
          </el-table-column>
          <el-table-column label="学习时间" width="180" align="center" prop="createTime"/>
          <el-table-column label="用时" align="center" prop="usedTime"/>
          <el-table-column label="详情" align="center" width="80" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-button link @click="getTestDetails(scope.row)" type="primary">查看</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
            style="margin-right: 20px"
            v-show="size > 0"
            :total="size"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="handleRecordData"
        />

      </el-col>
    </el-row>

    <el-dialog style="padding: 20px" title="学习记录" v-model="testResultWindow" width="900px" append-to-body>
      <el-table :data="testCompleteData">
        <el-table-column label="单词/词组" align="center" prop="name"/>
        <el-table-column label="词性以及释义" align="center" prop="chinese"/>
        <el-table-column label="测试时间" width="180" align="center" prop="time"/>
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup>
// 查询
import {getCurrentInstance, ref} from 'vue';
import {listStudy_record_ByUser} from "@/api/word/wordRecord.js";
import useUserStore from '@/store/modules/user'
import {isNumberStr} from "@/utils/index.js";
import {getPracticeListParam} from "@/api/cet4/index.js";

const {proxy} = getCurrentInstance();
let groupNum = ref(null)

// 弹窗
let correctNum = ref(0)
let testTotal = ref(0)
let testCompleteNum = ref(0)
let testCompleteData = ref([])
let testCompleteRate = ref(0)
let testResultWindow = ref(false)

let queryParam = ref({
  type: 'cet4',
  userId: useUserStore().id,
  pageSize: 10
})

let queryParams = ref({
  pageNum: 1,
  userId: useUserStore().id,
  pageSize: 10
})

let wordTestList = ref([])
let size = ref(0)

initPage()

function inputKeyUpEvent() {
  if (groupNum.value === '') {
    return
  }
  if (!isNumberStr(groupNum.value) || groupNum.value % 1 !== 0) {
    proxy.$modal.msgWarning("组号必须为整数")
    return
  }
  if (groupNum.value > 89 || groupNum.value < 1) {
    proxy.$modal.msgWarning("组号必须在1-89之间")
    return
  }
  queryParam.value.group = groupNum.value
}

//连接MYSQL数据库-查询全部
function handleRecordData() {
  // let queryForm = {'userId':useUserStore().id}
  // console.log(queryParams.value)
  listStudy_record_ByUser(queryParam.value).then(res => {
    if (res.code === 200) {
      wordTestList.value = res.rows;
      size.value = res.total;
      // console.log(res)
    }
  })
}

function getTestDetails(row) {
  // console.log(row)
  if (row.practice === undefined || row.practice === null) {
    proxy.$modal.msgWarning("此记录详情未开放")
    return
  }
  correctNum.value = row.correctNum
  testCompleteNum.value = row.complete
  testCompleteRate.value = parseInt(row.correctRate * 100)
  let query = {
    practice : row.practice,
  }

  getPracticeListParam(query).then((res) => {
    if (res.code === 200) {
      console.log(res)
      testResultWindow.value = true
      testCompleteData.value = res.data
      testTotal.value = res.data.length
    }
  })
}

function initPage() {
  let initQueryParam = {
    userId: useUserStore().id,
    type: 'cet4',
    pageNum: 1,
    pageSize: 10
  }
  listStudy_record_ByUser(initQueryParam).then(res => {
    if (res.code === 200) {
      wordTestList.value = res.rows;
      size.value = res.total;
      // console.log(res)
    }
  })
}

function resetQuery() {
  initPage()
}

function searchByUnit() {
  proxy.$modal.msg("查询的组号:" + groupNum.value)
  handleRecordData()
}

</script>

<style scoped lang="scss">
.header {
  margin-top: 20px;
  margin-left: 20px;
}
</style>
